<?php

declare (strict_types=1);

namespace app\base\controller\api;


use app\base\service\TokenService;
use think\admin\Controller;


/**
 * 业务基类
 * @class Package
 * @package app\base\controller
 */
class Auth extends Controller
{
    /**
     * 当前用户UID
     * @var int
     */
    protected $user_id;

    /**
     * 当前用户数据
     * @var array
     */
    protected $user;

    public function initialize()
    {
        parent::initialize();
        $this->checkToken();


    }


    /**
     * 检查报文中的token
     */
    protected function checkToken(){
       $header = $this->app->request->header();
        if(!isset($header['token']) || ($user_id = TokenService::instance()->token_parse($header['token'])) === false){
            $this->error('need token！', '', 403);
        }
        $this->user_id = $user_id;
        $user = $this->app->db->name("ai_user")->where(['id' => $user_id])->find();
        $this->user = $user;
        return;
    }






}
